﻿namespace Fiddler
{
    using System;
    using System.IO;

    internal class PipeReadBuffer : MemoryStream
    {
        private const int LARGE_OBJECT_HEAP_SIZE = 0x14000;
        private const int ONE_GIG = 0x3fffffff;
        private const int SIXTYFOUR_MEG = 0x4000000;

        public PipeReadBuffer(bool bIsRequest) : base(bIsRequest ? 0x2000 : 0x4000)
        {
        }

        public PipeReadBuffer(int iDefaultCapacity) : base(iDefaultCapacity)
        {
        }

        internal void HintTotalSize(int iHint)
        {
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            int capacity = base.Capacity;
            uint num2 = (uint) (base.Position + count);
            if (num2 > capacity)
            {
                if (num2 < 0x14000)
                {
                    if ((capacity * 2) > 0x14000)
                    {
                        this.Capacity = 0x14000;
                    }
                }
                else if (num2 > 0x3fffffff)
                {
                    uint num3 = num2 + 0x4000000;
                    if (num3 >= 0x7fffffff)
                    {
                        throw new Exception("Sorry, the .NET Framework (and Fiddler) cannot handle streams larger than 2 Gigabytes.");
                    }
                    this.Capacity = (int) num3;
                }
            }
            base.Write(buffer, offset, count);
        }
    }
}

